package vn.edu.tdt.it.domain;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import vn.hus.nlp.tokenizer.VietTokenizer;
import de.tudarmstadt.ukp.wikipedia.api.DatabaseConfiguration;
import de.tudarmstadt.ukp.wikipedia.api.Page;
import de.tudarmstadt.ukp.wikipedia.api.WikiConstants.Language;
import de.tudarmstadt.ukp.wikipedia.api.Wikipedia;

public class TestDictionary {
	public static void main(String[] args) throws Exception {
		long start = System.currentTimeMillis();
		List<String> stopword = readStopword();
		DatabaseConfiguration dbConfig = new DatabaseConfiguration("localhost", "wiki", "root", "", Language.vietnamese);
		Wikipedia wiki = new Wikipedia(dbConfig);
		
		
		StaticConnection.DB_URL = "jdbc:mysql://localhost/wiki?characterEncoding=UTF-8";
		String test = wiki.getPage("Một_nhà_tự_chia_rẽ").getPlainText();
		VietTokenizer token = new VietTokenizer();
		token.turnOffSentenceDetection();
		String[] tokened = token.tokenize(test);
		tokened = tokened[0].split(" ");
		List<String> lstTokened = new ArrayList<String>(Arrays.asList(tokened));
		System.out.println(lstTokened.size());
		removeStopword(lstTokened,stopword);
		System.out.println(lstTokened.size());
		System.out.println(tokened);
		int res = -1;
		String sql = "Select pageId from pagemapline where name = ?";
		PreparedStatement p = StaticConnection.getPreparedStatement(sql);
		List<Integer> checked = new ArrayList<Integer>();
		List<String> inputed = new ArrayList<String>();
		System.err.println("--------------------5-gram-----------------------");
		for (int i=0; i<lstTokened.size()-4; i++) {
			if(checked.contains(i)) continue;
			if(inputed.contains(lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2)+"_"+lstTokened.get(i+3)+"_"+lstTokened.get(i+4))) continue;
			inputed.add(lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2)+"_"+lstTokened.get(i+3)+"_"+lstTokened.get(i+4));
			//System.out.println("checking: " + lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2)+"_"+lstTokened.get(i+3));
			p.setString(1, lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2)+"_"+lstTokened.get(i+3)+"_"+lstTokened.get(i+4));
			ResultSet rs = p.executeQuery();
			if (rs.next()) {
				checked.add(i);
				checked.add(i+1);
				checked.add(i+2);
				checked.add(i+3);
				checked.add(i+4);
				res = rs.getInt(1);
				Page page = wiki.getPage(res);
				System.out.println("Input: " + lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2)+"_"+lstTokened.get(i+3) + "_"+lstTokened.get(i+4) + "  Result: " + res + " Page name: " + page.getTitle());
			}
		}
		System.err.println("--------------------4-gram-----------------------");
		for (int i=0; i<lstTokened.size()-3; i++) {
			if(checked.contains(i)) {
				//System.out.println("Loại: " + lstTokened.get(i));
				continue;
			}
			if(inputed.contains(lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2)+"_"+lstTokened.get(i+3))) {
				//System.out.println("Loại: " + lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2)+"_"+lstTokened.get(i+3));
				continue;
			}
			inputed.add(lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2)+"_"+lstTokened.get(i+3));
			//System.out.println("checking: " + lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2)+"_"+lstTokened.get(i+3));
			p.setString(1, lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2)+"_"+lstTokened.get(i+3));
			ResultSet rs = p.executeQuery();
			if (rs.next()) {
				checked.add(i);
				checked.add(i+1);
				checked.add(i+2);
				checked.add(i+3);
				res = rs.getInt(1);
				Page page = wiki.getPage(res);
				System.out.println("Input: " + lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2)+"_"+lstTokened.get(i+3) + "  Result: " + res + " Page name: " + page.getTitle());
			}
		}
		
		System.err.println("--------------------3-gram-----------------------");
		for (int i=0; i<lstTokened.size()-2; i++) {
			if(checked.contains(i)) {
				//System.out.println("Loại: " + lstTokened.get(i));
				continue;
			}
			if(inputed.contains(lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2))) {
				//System.out.println("Loại: " + lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2));
				continue;
			}
			inputed.add(lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2));
			//System.out.println("checking: " + lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2));
			p.setString(1, lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2));
			ResultSet rs = p.executeQuery();
			if (rs.next()) {
				checked.add(i);
				checked.add(i+1);
				checked.add(i+2);
				res = rs.getInt(1);
				Page page = wiki.getPage(res);
				System.out.println("Input: "+lstTokened.get(i)+"_"+lstTokened.get(i+1)+"_"+lstTokened.get(i+2)+"  Result: " + res + " Page name: " + page.getTitle());
			}
		}
		
		System.err.println("--------------------2-gram-----------------------");
		for (int i=0; i<lstTokened.size()-1; i++) {
			if(checked.contains(i)) {
				//System.out.println("Loại: " + lstTokened.get(i));
				continue;
			}
			if(inputed.contains(lstTokened.get(i)+"_"+lstTokened.get(i+1))) {
				//System.out.println("Loại: " + lstTokened.get(i)+"_"+lstTokened.get(i+1));
				continue;
			}
			inputed.add(lstTokened.get(i)+"_"+lstTokened.get(i+1));
			//System.out.println("checking: " + lstTokened.get(i)+"_"+lstTokened.get(i+1));
			p.setString(1, lstTokened.get(i)+"_"+lstTokened.get(i+1));
			ResultSet rs = p.executeQuery();
			if (rs.next()) {
				checked.add(i);
				checked.add(i+1);
				res = rs.getInt(1);
				Page page = wiki.getPage(res);
				System.out.println("Input: "+lstTokened.get(i)+"_"+lstTokened.get(i+1) + "  Result: " + res + " Page name: " + page.getTitle());
			}
		}
		System.err.println("--------------------1-gram-----------------------");
		for (int i=0; i<lstTokened.size(); i++) {
			//System.out.println("checking: " + lstTokened.get(i));
			if(checked.contains(i)) {
				//System.out.println("Loại: " + lstTokened.get(i));
				continue;
			}
			if(inputed.contains(lstTokened.get(i))) {
				//System.out.println("Loại: " + lstTokened.get(i));
				continue;
			}
			inputed.add(lstTokened.get(i));
			p.setString(1, lstTokened.get(i));
			ResultSet rs = p.executeQuery();
			if (rs.next()) {
				checked.add(i);
				res = rs.getInt(1);
				Page page = wiki.getPage(res);
				System.out.println("Input: "+lstTokened.get(i)+"  Result: " + res + " Page name: " + page.getTitle());
			}
		}
		System.out.println("total time: " + (System.currentTimeMillis()-start));
	}

	private static void removeStopword(List<String> lstTokened,
			List<String> stopword) {
		for(String s: stopword) {
			while(lstTokened.remove(s));
		}
	}

	private static List<String> readStopword() throws Exception {
		ArrayList<String> arlist = new ArrayList<String>();
		BufferedReader fr = new BufferedReader(new FileReader("resources/stopword.txt"));
		String line = fr.readLine();
		while (line != null) {
			arlist.add(line);
			line = fr.readLine();
		}
		return arlist;
	}
	
	/*private static void nGramCheck(List<String> lstTokened) {
		for (int i=0; i<lstTokened.size()-1; i++) {
			System.out.println("checking: " + lstTokened.get(i)+"_"+lstTokened.get(i+1));
			p.setString(1, tokened[i]+"_"+tokened[i+1]);
			ResultSet rs = p.executeQuery();
			if (rs.next()) {
				res = rs.getInt(1);
				Page page = wiki.getPage(res);
				System.out.println("Result: " + res + " Page name: " + page.getTitle());
			}
		}
	}*/
	private static boolean isNumber(String ck) {
		try {
			Integer.parseInt(ck);
		} catch (Exception ex) {
			return false;
		}
		return true;
	}
}

